package cn.cqautotest.cqviehelper.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.blankj.utilcode.util.ToastUtils;
import cn.cqautotest.cqviehelper.config.Constants;
import cn.cqautotest.cqviehelper.ui.activity.WebViewActivity;
import cn.cqautotest.cqviehelper.ui.dialog.MessageDialog;
import cn.cqautotest.cqviehelper.ui.dialog.ToastDialog;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

public class StartAppUtils {

    private static boolean isNotOk = true;

    /**
     * 跳转到支付宝转账界面
     *
     * @param context
     * @return isStartOk
     */
    public static boolean startAliPay(Context context) {
        isNotOk = true;
        boolean apkIsExist = GetAppInfoUtils.checkApkExist(context, "com.eg.android.AlipayGphone");
        new MessageDialog.Builder(context)
                .setTitle("捐赠")
                .setMessage("如果你觉得这个项目很棒，希望它能更好地坚持开发下去，可否愿意花一点点钱（推荐 10.24 元）作为对于开发者的激励")
                .setConfirm("支付宝")
                .setCancel(null)
                //.setAutoDismiss(false)
                .setListener(dialog -> {
                    try {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.ALIPAY_URL));
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        new ToastDialog.Builder(context).setMessage("感谢您的支持").create().show();
                        isNotOk = false;
                    } catch (Exception e) {
                        new ToastDialog.Builder(context).setMessage(apkIsExist ? "打开支付宝失败" : "检测到您未安装支付宝~").create().show();
                        //跳转到二维码支付页面
                        Intent intent = new Intent(context, WebViewActivity.class);
                        intent.putExtra(Constants.STR_URL, Constants.PAY_QR_URL);
                        intent.putExtra(Constants.STR_TITLE, "向我捐赠");
                        startActivity(intent);
                    }
                }).show();
        return !isNotOk;
    }
}
